package com.kedacom.demo.app;

import com.kedacom.demo.sigar.cloudify.MonitorData;
import com.kedacom.demo.sigar.cloudify.MonitorException;
import com.kedacom.demo.sigar.util.SigarUtil;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.hyperic.sigar.Sigar;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Wrapper {

    private static final Logger logger = Logger.getLogger(Wrapper.class.getName());

    public static void main(String[] args) {
        logger.info("App Init");
        Thread monitorThread = new Thread(new MonitorRunnable(), "MonitorThread");
        logger.info("Start MonitorThread");
        //monitorThread.start();
        logger.info("开如初始化Spring容器.");

        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("/applicationContext.xml");
        //AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();

        //ctx.scan("com.kedacom.*");
        //ctx.refresh();
        //ctx.start();
        //PublishMyEventBean mb = (PublishMyEventBean) ctx.getBean("publishMyEventBean");
        //mb.doSomething();

        //ctx.stop();
        //ctx.close();


        Sigar sigar = SigarUtil.getSigarInstance();
        try {
            MonitorData md = new MonitorData(sigar, sigar.getPid());
            logger.info(md.getDataMap().toString());

        }
        catch (MonitorException ex) {
            logger.log(Level.SEVERE, null, ex);
        }


        //logger.info("Spring容器初始化完毕.");

        /*

         Message message = (Message) applicationContext.getBean("message");

         logger.debug("message='" + message.getMessage() + "'");
         */


        //需要提纯的东东
        /*
         //SigarUtil.registSigarMBeans();
         //HazelcastUtil.getHazelcastInstance();

         //关闭处理
         Runtime.getRuntime().addShutdownHook(new Thread() {
         @Override
         public void run() {
         //do some this
         }
         });

         MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
         HelloWorld hw = new HelloWorld();
         ObjectName helloWorldName = null;
         try {
         helloWorldName = new ObjectName("JavaServiceDemo :name=JavaServiceAppConfig");
         }
         catch (MalformedObjectNameException ex) {
         Logger.getLogger(Wrapper.class.getName()).log(Level.SEVERE, null, ex);
         }
         catch (NullPointerException ex) {
         Logger.getLogger(Wrapper.class.getName()).log(Level.SEVERE, null, ex);
         }
         try {
         mbs.registerMBean(hw, helloWorldName);
         }
         catch (InstanceAlreadyExistsException ex) {
         Logger.getLogger(Wrapper.class.getName()).log(Level.SEVERE, null, ex);
         }
         catch (MBeanRegistrationException ex) {
         Logger.getLogger(Wrapper.class.getName()).log(Level.SEVERE, null, ex);
         }
         catch (NotCompliantMBeanException ex) {
         Logger.getLogger(Wrapper.class.getName()).log(Level.SEVERE, null, ex);
         }
         */

    }
}
